package com.stars.home.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import com.stars.home.R
import com.stars.home.databinding.FragmentDayRightBinding
import com.stars.home.inte.ChoiceListData
import com.stars.home.view.timeview.OnWheelTimeScrollListener
import com.stars.home.view.timeview.WheelTimeView
import com.stars.home.view.timeview.adapter.TimeNumericWheelAdapter
import com.trailer.base.view.BView
import com.trailer.base.view.BaseView
import java.lang.String
import java.util.*

/**
 *选择天
 *on 2020/11/23
 */
class TimeDayView(context: Context, attrs: AttributeSet) :BaseLinearLayout(context, attrs) {

    private var binding: FragmentDayRightBinding? = null

    private var view: BView? = null

    init {
        init(context)

    }

    private fun init(context: Context) {
        if (context is BaseView) {
            view = context as BView
        }
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.fragment_day_right, this, true)
        setDate()
    }



    var choiceTimeLeftData: ChoiceListData? =null


    var  time=""
    fun  setCallBack(choiceData: ChoiceListData){
        this.choiceTimeLeftData=choiceData

        if(null!=choiceData){

            val c: Calendar = Calendar.getInstance()
            val curYear: Int = c.get(Calendar.YEAR)


             time = String.format(
                    Locale.CHINA, "%04d-%02d", binding!!.year.currentItem+curYear-5 ,
                    binding!!.month.currentItem + 1
            )
            choiceData.onRestuse("1",time)

        }
    }

    fun getTimeContent():kotlin.String{
        return time
    }

    fun setDate(){
        val c: Calendar = Calendar.getInstance()
        val curYear: Int = c.get(Calendar.YEAR)-5
        val curMonth: Int = c.get(Calendar.MONTH) + 1 //通过Calendar算出的月数要+1

        initYear()
        initMonth()

        binding!!.year.currentItem = curYear-6
        binding!!.month.currentItem = curMonth - 1
        binding!!.year.visibleItems = 7
        binding!!.month.visibleItems = 7


    }

    /**
     * 初始化年
     */
    private fun initYear() {
        val c: Calendar = Calendar.getInstance()
        val curYear: Int = c.get(Calendar.YEAR)-5

        val numericWheelAdapter = TimeNumericWheelAdapter(context, curYear, 2050)
        numericWheelAdapter.setLabel(" 年")
        numericWheelAdapter.textSize = 18
        numericWheelAdapter.textColor = resources.getColor(R.color.pay_off)

        binding!!.year.viewAdapter = numericWheelAdapter
        binding!!.year.isCyclic = true
        binding!!.year.addScrollingListener(object : OnWheelTimeScrollListener {
            override fun onScrollingStarted(wheel: WheelTimeView?) {

            }

            override fun onScrollingFinished(wheel: WheelTimeView?) {

                val c: Calendar = Calendar.getInstance()
                val curYear: Int = c.get(Calendar.YEAR)

                val time = String.format(
                        Locale.CHINA, "%04d-%02d", binding!!.year.currentItem + curYear-5,
                        binding!!.month.currentItem + 1)


                if(null!=choiceTimeLeftData){

                    choiceTimeLeftData!!.onRestuse("1",time)

                }

            }

        })
    }
    /**
     * 初始化月
     */
    private fun initMonth() {
        val numericWheelAdapter = TimeNumericWheelAdapter(context, 1, 12, "%02d")
        numericWheelAdapter.setLabel(" 月")
        numericWheelAdapter.textSize = 18
        numericWheelAdapter.textColor= resources.getColor(R.color.pay_off)
        binding!!.month.viewAdapter = numericWheelAdapter
        binding!!.month.isCyclic = true
        binding!!.month.addScrollingListener(object : OnWheelTimeScrollListener {
            override fun onScrollingStarted(wheel: WheelTimeView?) {

            }

            override fun onScrollingFinished(wheel: WheelTimeView?) {

                val c: Calendar = Calendar.getInstance()
                val curYear: Int = c.get(Calendar.YEAR)


                val time = String.format(
                        Locale.CHINA, "%04d-%02d", binding!!.year.currentItem + curYear-5,
                        binding!!.month.currentItem + 1
                )
                if(null!=choiceTimeLeftData){

                    choiceTimeLeftData!!.onRestuse("1",time)

                }


            }

        })

    }




}
